package com.tmpt.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import com.inspiresoftware.lib.dto.geda.adapter.BeanFactory;
import com.inspiresoftware.lib.dto.geda.assembler.DTOAssembler;
import com.tmpt.beans.*;
import com.tmpt.dao.impl.TemplateUtils;
import com.tmpt.dao.intf.*;
import com.tmpt.dto.*;
import com.tmpt.others.pageUtils.PaginationService;
import com.tmpt.service.ILayoutUIService;
import com.tmpt.service.ITemplateService;
import com.tmpt.service.utils.Column_Constant;
import com.tmpt.service.utils.SpringContextHolder;
import com.tmpt.utils.Config;
import com.tmpt.utils.FenAndYuan;
import com.tmpt.utils.JsonMap;
import com.tmpt.utils.StringUtils;
import com.tmptSource.bean.A1templatecolumn;
import net.sf.json.JSON;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.*;

@Service("layoutUIService")
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class LayoutUIService implements ILayoutUIService {



	private Logger log = Logger.getLogger(this.getClass());

	@Resource private IA1TemplateTableDao a1TemplateTableDao;



	@Override
	public Result saveStyle(String... paramaters) {
		Result result = new Result();
		//取参数
		String keyRandom = paramaters[0];
		String uiStyle = paramaters[1];
		String layoutStyle = paramaters[2];
		String isdialog4AddDialog = paramaters[3];
		int isdialog4AddDialogInt = 1;
		try{
			isdialog4AddDialogInt = Integer.parseInt(isdialog4AddDialog);
		}catch (Exception e){
			isdialog4AddDialogInt = 1;
		}
		//[{"columnNameShow":"ID","columnName":"id","columnUIClass":"easyui-combobox","columnLayoutStyle":"width: 300px; height: 30px; position: relative;"},{"columnNameShow":"模式","columnName":"mode","columnUIClass":"easyui-textbox","columnLayoutStyle":"width: 460.4px; height: 30.4px;"},{"columnNameShow":"hasHistoryBtn","columnName":"hashistorybtn","columnTip":"","columnUIClass":"easyui-passwordbox","dataOptions":"{\"height\":40,\"width\":300,\"required\":true,\"multiline\":true}","columnLayoutStyle":"width: 420.4px; height: 60.4px;"}]
		//{"width":"700px"}
		/*JSONObject layoutStyleJson = new JSONObject(layoutStyle);
		int layoutWidth = layoutStyleJson.optInt("width");
		int layoutHeight = layoutStyleJson.optInt("height");*/
		JSONArray uiStyleJArr = new JSONArray(uiStyle);
		//.out.println(uiStyle);
		//.out.println(layoutStyle);
		//.out.println(isdialog4AddDialog);

		//
		Search search = new Search();
		search.addFilterEqual("keyRandom", keyRandom);
		A1TemplateTable table = (A1TemplateTable)a1TemplateTableDao.searchUnique(search);
		if(table==null)return result.fault("keyRandom无效");
		//设置dialog宽高
		if(!StringUtils.isNull(layoutStyle)){
			table.setStyle4AddDialog(layoutStyle);
			table.setIsdialog4AddDialog(isdialog4AddDialogInt);
		}
		//设置列样式
		List<A1TemplateColumn> columnList = table.getA1TemplateColumnList();
		for(A1TemplateColumn column:columnList){
			column.setCanAdd(0);
		}
		//从100开始sort
		int sort =100;
		for(int i=0;i<uiStyleJArr.length();i++){
			JSONObject jsonStyle = uiStyleJArr.optJSONObject(i);
			String columnNameShow = jsonStyle.optString("columnNameShow");
			String columnName = jsonStyle.optString("columnName");
			String columnTip = jsonStyle.optString("columnTip");
			String columnUIClass = jsonStyle.optString("columnUIClass");
			String dataOptions = jsonStyle.optString("dataOptions");
			String columnLayoutStyle = jsonStyle.optString("columnLayoutStyle");
			//操作列
			for(A1TemplateColumn column:columnList){
				String columnName_db = column.getColumnName();
				if(columnName_db.equals(columnName)){
					column.setColumnShowName(columnNameShow);
					column.setColumnTip(columnTip);
					column.setDataOptions4AddDialog(dataOptions);
					column.setColumnLayoutStyle4adddialog(columnLayoutStyle);
					column.setSort(sort);
					column.setCanAdd(1);
					sort = sort+100;
					column.setColumnType(columnUIClass);
					break;
				}
			}
		}

		return result.success();
	}


}
